<!DOCTYPE html>
<html>
<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>客户体检信息健康管理系统</title>
    <meta name="description" content="客户体检信息健康管理系统">
    <meta name="keywords" content="客户体检信息健康管理系统">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/elementui/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">
    <!-- 引入组件库 -->
    <script type="text/javascript" src="../js/jquery.min.js"></script>
    <script src="../js/vue.js"></script>
    <script src="../plugins/elementui/index_copy.js"></script>
    <script src="../js/axios-0.18.0.js"></script>
    <style>
        .datatable {
            position: relative;
            box-sizing: border-box;
            -webkit-box-flex: 1;
            width: 100%;
            max-width: 100%;
            font-size: 14px;
            color: rgb(96, 98, 102);
            overflow: hidden;
            flex: 1 1 0%;
        }

        .datatable td, .datatable th {
            padding: 12px 0;
            min-width: 0;
            -webkit-box-sizing: border-box;
            box-sizing: border-box;
            text-overflow: ellipsis;
            vertical-align: middle;
            position: relative;
            text-align: left;
        }
    </style>
</head>
<body class="hold-transition">
<div id="app">
    <div class="content-header">
        <h1>会员管理<small>会员档案</small></h1>
        <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>会员管理</el-breadcrumb-item>
            <el-breadcrumb-item>会员档案</el-breadcrumb-item>
        </el-breadcrumb>
    </div>
    <div class="app-container">
        <div class="box">
            <div class="filter-container">
                <!--档案号/-->
                <el-input placeholder="名称/手机号" v-model="pagination.queryString" style="width: 200px;"
                          class="filter-item" @keyup.enter.native="handleFilter"></el-input>
                <el-button @click="findPage()" class="dalfBut">查询</el-button>
                <el-button type="primary" class="butT" @click="handleCreate()">新建</el-button>
            </div>
            <el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>
                <el-table-column type="index" align="center" label="序号"></el-table-column>
<!--                <el-table-column prop="fileNumber" label="档案号" align="center"></el-table-column>-->
                <el-table-column prop="name" label="姓名" align="center"></el-table-column>
                <el-table-column label="性别" align="center">
                    <template slot-scope="scope">
                        <span>{{ scope.row.sex == '0' ? '不限' : scope.row.sex == '1' ? '男' : '女'}}</span>
                    </template>
                </el-table-column>
<!--                <el-table-column prop="idCard" label="身份证" align="center"></el-table-column>-->
                <!--                <el-table-column prop="username" label="健康管理师" align="center"></el-table-column>-->
                <el-table-column prop="phoneNumber" label="手机号码" align="center"></el-table-column>
                <el-table-column prop="regTime" label="注册时间" align="center"></el-table-column>
                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                        <!--                                通过scop.row拿取当前这条列的数据，对应与这条列的json对象-->
                        <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
                        <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>

            <div class="pagination-container">
                <el-pagination
                        class="pagiantion"
                        @current-change="handleCurrentChange"
                        :current-page="pagination.currentPage"
                        :page-size="pagination.pageSize"
                        layout="total, prev, pager, next, jumper"
                        :total="pagination.total">
                </el-pagination>
            </div>
            <!-- 新增标签弹层 -->
            <div class="add-form">
                <el-dialog title="新增会员" :visible.sync="dialogFormVisible">
                    <template>
                        <el-form ref="dataAddForm" :model="formData" :rules="rules" label-position="right"
                                 label-width="100px">
                            <el-row>
                                <!--<el-col :span="12">
                                    <el-form-item label="档案号" prop="fileNumber">
                                        <el-input v-model="formData.fileNumber"/>
                                    </el-form-item>
                                </el-col>-->
                                <el-col :span="12">
                                    <el-form-item label="姓名" prop="name">
                                        <el-input v-model="formData.name"/>
                                    </el-form-item>
                                </el-col>
                            </el-row>
                            <el-row>
                                <el-col :span="12">
                                    <el-form-item label="性别" prop="sex">
                                        <el-select v-model="formData.sex">
                                            <el-option label="男" value="1"></el-option>
                                            <el-option label="女" value="2"></el-option>
                                        </el-select>
                                    </el-form-item>
                                </el-col>
                                <el-col :span="12">
                                    <el-form-item label="手机号码" prop="phoneNumber">
                                        <el-input v-model="formData.phoneNumber"/>
                                    </el-form-item>
                                </el-col>
                            </el-row>
                            <el-row>
                                <!-- <el-col :span="12">
                                     <el-form-item label="健康管理师">
                                         <el-input v-model="formData.username"/>
                                     </el-form-item>
                                 </el-col>-->
                                <el-col :span="12">
                                    <el-form-item label="邮箱">
                                        <el-input v-model="formData.email"/>
                                    </el-form-item>
                                </el-col>
                            </el-row>
                            <el-row>
                                <el-col :span="12">
                                    <el-form-item label="出生日期" prop="birthday">
                                        <el-date-picker type="date" v-model="formData.birthday"></el-date-picker>
                                    </el-form-item>
                                </el-col>
                                <!--<el-col :span="12">
                                    <el-form-item label="身份证号" prop="idCard">
                                        <el-input v-model="formData.idCard"/>
                                    </el-form-item>
                                </el-col>-->
                            </el-row>
                            <el-row>
                                <el-col :span="24">
                                    <el-form-item label="备注" prop="remark">
                                        <el-input v-model="formData.remark" type="textarea"></el-input>
                                    </el-form-item>
                                </el-col>
                            </el-row>
                        </el-form>
                    </template>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible = false">取消</el-button>
                        <el-button type="primary" @click="handleAdd()">确定</el-button>
                    </div>
                </el-dialog>
            </div>

            <!-- 编辑标签弹层 -->
            <div class="add-form">
                <el-dialog title="编辑检查项" :visible.sync="dialogFormVisibleEdit">
                    <el-form ref="dataAddForm" :model="formData" :rules="rules" label-position="right"
                             label-width="100px">
                        <el-row>
                            <!--<el-col :span="12">
                                <el-form-item label="档案号" prop="fileNumber">
                                    <el-input v-model="formData.fileNumber"/>
                                </el-form-item>
                            </el-col>-->
                            <el-col :span="12">
                                <el-form-item label="姓名" prop="name">
                                    <el-input v-model="formData.name"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="性别" prop="sex">
                                    <el-select v-model="formData.sex">
                                        <el-option label="男" value="1"></el-option>
                                        <el-option label="女" value="2"></el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="手机号码" prop="phoneNumber">
                                    <el-input v-model="formData.phoneNumber"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <!-- <el-col :span="12">
                                 <el-form-item label="健康管理师">
                                     <el-input v-model="formData.username"/>
                                 </el-form-item>
                             </el-col>-->
                            <el-col :span="12">
                                <el-form-item label="邮箱">
                                    <el-input v-model="formData.email"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="出生日期" prop="birthday">
                                    <el-date-picker type="date" v-model="formData.birthday"></el-date-picker>
                                </el-form-item>
                            </el-col>
                            <!--<el-col :span="12">
                                <el-form-item label="身份证号" prop="idCard">
                                    <el-input v-model="formData.idCard"/>
                                </el-form-item>
                            </el-col>-->
                        </el-row>
                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="备注" prop="remark">
                                    <el-input v-model="formData.remark" type="textarea"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisibleEdit = false">取消</el-button>
                        <el-button type="primary" @click="handleEdit()">确定</el-button>
                    </div>
                </el-dialog>
            </div>

        </div>
    </div>
</div>
</body>

<script>
    var vue = new Vue({
        el: '#app',
        data: {
            activeName: 'first',//添加/编辑窗口Tab标签名称
            pagination: {//分页相关属性
                currentPage: 1,
                pageSize: 10,
                total: 100,
                queryString: null,
            },
            checked: true,
            dataList: [],//列表数据
            formData: {},//表单数据
            station: false,
            parentMenuList: [],
            tableData: [],//新增和编辑表单中对应的检查项列表数据
            roleIds: [],//新增和编辑表单中角色对应的复选框，基于双向绑定可以进行回显和数据提交
            dialogFormVisible: false,//控制添加窗口显示/隐藏
            dialogFormVisibleEdit: false,//控制编辑窗口显示/隐藏
            rules: {//校验规则
                /*fileNumber: [{
                    required: true, message: '档案号为必填项', trigger: 'blur',
                }],*/
                name: [{required: true, message: '姓名为必填项', trigger: 'blur'}],
                sex: [{required: true, message: '性别为必选项', trigger: 'change'}],
                phoneNumber: [{
                    required: true, message: '手机号码为必填项', trigger: 'blur'
                }, {min: 11, max: 11, message: '手机号码格式错误', trigger: 'blur'}],
                /*idCard: [{
                    required: true, message: '身份证为必填项', trigger: 'blur'
                }],*/
                birthday: [{required: true, message: '出生日期为必选项', trigger: 'blur'}],
                remark: [{required: true, message: '备注为必填项', trigger: 'blur'}]
            }
        },
        created() {
            this.findPage();
        },
        methods: {
            changeStatus() {
                this.station = !!this.station
                this.formData.station = 1 ? this.station : !!this.station
            },
            //编辑
            handleEdit() {
                //表单校验
                this.$refs['dataAddForm'].validate((valid) => {
                    if (valid) {
                        if (this.roleIds.length > 1) {
                            this.$message({
                                message: '一个会员只能拥有一个角色',
                                type: 'warning'
                            });
                            return;
                        }
                        //表单校验通过，发送请求
                        axios.post("/member/edit.do", this.formData).then((res) => {
                            if (res.data.flag) {
                                //controller操作成功
                                this.$message({
                                    type: 'success',
                                    message: res.data.message
                                });
                            } else {
                                //执行失败
                                this.$message.error(res.data.message);
                            }
                        }).finally(() => {
                            //finally不管成功还是失败都要调用分页查询方法
                            this.findPage();
                            //隐藏编辑窗口
                            this.dialogFormVisibleEdit = false;
                        });
                    }else{
                        return false;
                    }
                });

            },
            //添加
            handleAdd() {
                //表单校验
                this.$refs['dataAddForm'].validate((valid) => {
                    if (valid) {
                        //表单校验通过，发送ajax请求，将录入的数据提交给后台controller进行处理
                        // console.log(this.formData);
                        axios.post("/member/add.do", this.formData).then((res) => {
                            //关闭新增窗口，不管成功或者是失败都要将窗口关闭掉
                            this.dialogFormVisible = false;
                            if (res.data.flag) {//执行成功
                                // console.log(res);
                                //新增成功后，重新调用分页查询方法。查询出最新的数据
                                this.findPage();
                                //弹出提示信息
                                this.$message({//$message是vue的Element组件Message的 消息提示
                                    message: res.data.message,
                                    type: 'success'
                                });
                            } else {//执行失败
                                //弹出提示，$message是vue的Element组件Message的 消息提示
                                this.$message.error(res.data.message);
                            }
                        }).catch((r) => {
                            this.showMessage(r);
                        });
                    } else {
                        //校验不通过
                        // this.$message.error("数据校验失败，请检查你输入的信息是否正确！");
                        //失败了，剩下的代码就没有必要执行了，所以
                        return false;
                    }
                })

            },
            //分页查询
            async findPage() {
                let result = await axios.post("/member/findPage.do", this.pagination);
                // console.log(result)
                this.dataList = result.data.rows
                for (let i = 0; i < this.dataList.length; i++) {
                    this.dataList[i].regTime = this.formatDate(this.dataList[i].regTime)
                }
                this.pagination.total = result.data.total
            },
            // 重置表单
            resetForm() {
                this.formData = {};
            },
            // 弹出添加窗口
            handleCreate() {
                this.resetForm()
                this.dialogFormVisible = true
            },
            // 弹出编辑窗口
            handleUpdate(row) {
                // console.log(row)
                this.formData = row
                this.dialogFormVisibleEdit = true
            },
            //切换页码
            handleCurrentChange(currentPage) {
                this.pagination.currentPage = currentPage
                this.findPage()
            },
            // 删除
            handleDelete(row) {
                // console.log(row)
                this.$confirm('确认删除吗？', '提示', {type: 'warning'}).then(async () => {
                    let result = await axios.get("/member/delete.do?id=" + row.id);
                    if (!result.data.flag) {
                        //删除失败
                        this.$message.error(result.data.message)
                    } else {
                        //删除成功
                        this.$message({
                            message: result.data.message,
                            type: 'success'
                        });
                        await this.findPage();

                    }
                }).catch((error) => {

                })
            },
            //日期转字符串格式
            formatDate(dateStr) {
                let newdateStr = dateStr.replace(/\d+(?=-[^-]+$)/, (a) => (parseInt(a, 10) - 1));  //字符串处理(月份)
                let nums = newdateStr.match(/\d+/g);  //取数
                let date = eval('new Date(' + nums + ')');  //转换
                //重组
                let datetime =
                    date.getFullYear() + "年" +  //年
                    ((date.getMonth() + 1) > 9 ? (date.getMonth() + 1) : "0" + (date.getMonth() + 1)) + "月" +  //月
                    (date.getDate() > 9 ? date.getDate() : ("0" + date.getDate())) + "日";  //日
                return datetime;
            }
        }
    })
</script>
</html>
